var typeid = 0;
var List = [];
var startDate = '';
var endDate = '';
var editUserId = 0;
var newscore = 0;
var userid = 0;
var sendState = -1;

var title = '';
var username = '';

//分页查询
var limit = 5;// 每页多少条
var currPage = 1;// 当前页
var allPage = 1;// 总页数
var count = 0;// 默认共多少条数据

function getstartDate(){
	startDate = document.getElementById('start').value;
	
	
}
function getendDate(){
	endDate = document.getElementById('end').value;
	
}

function doSend(){
	title = $('#title').val();
	username = $('#username').val();
	selectCheckFile();
}

function changeSendState(){
	sendState = $('#sendState').val();
	console.log('寄送状态:'+sendState);
}


selectCheckFile();
function selectCheckFile(){
	console.log(startDate);
	console.log(endDate);


	$.ajax({
		url:ajax_host+'selectSendFile.act',
		dataType : 'JSON',
		data:{
			title : title,
            username : username,
			start : startDate,
			end : endDate,
			startpage : currPage,
			limitpage : limit,
			sendState : sendState
		},
		success:function(resp){
			
			if(resp.success){
				List = resp.list;
				console.log("查询后列表："+List);
				renderTable(resp.list);
				// 总页数=记录数/每页记录
				count = resp.count;
				console.log('总记录数:'+count)
				allPage=count%limit==0?(count/limit):(parseInt(count/limit+1));
				console.log('总页数:'+allPage);
				$("#pager").html(currPage+"/"+allPage);
			}else{
				layer.alert(resp.errmsg);
			}
        }
	})
}

function renderTable(arr){
	var str = `<tr>
	<th>寄件人</th>
    <th>快件单号</th>
	<th>快件名</th>
    <th>收件人</th>
    <th>审核状态</th>
    <th>快件目的地</th>
    <th>快件类型</th>
	<th>寄送状态</th>
	<th>寄件所在网点</th>
	<th>操作</th>
	</tr>`;
	for(var i = 0;i<arr.length;i++){
		var a = arr[i];
		str += 
		   `<tr>
		   <td>${a.uName}</td>
		   <td>${a.eId}</td>
		   <td>${a.eName}</td>
		   <td>${a.aTtn}</td>
		   <td>`;
		   if(a.cId == 1) str +='待审核';
		   if(a.cId == 2) str +='审核通过';
		   if(a.cId == 3) str +='审核不通过';
		   str +=`</td>
		   <td>${a.dest}</td>
		   <td>${a.tName}</td>
		   <td>`;
		   if(a.sendState == 0) str += '可寄送';
		   if(a.sendState == 1) str += '要求退单';
		   if(a.sendState == 2) str += '已退单';
		   if(a.sendState == 3) str += '已寄送';
		   if(a.sendState == 4) str += '寄送失败';
		   str +=`</td>
		   <td>${a.uNidame}</td>
			<td>`;
			if(a.sendState == 0){
				str += `<a href = "###" onclick="sendfile(${i},${a.uId},${a.eId},${a.tMoney})">进行发件</a>`;
				str +=`</td>`
			}
			if(a.sendState == 1){
				str += `<a href = "###" onclick="tuidan(${a.uId},${a.eId},${a.tMoney})">进行退单</a>`;
				str +=`</td>`
			}

			

            
			str+=`</tr>`;
		
	}
	html('table',str);
}




//分页操作
function pagenext() {
	if (currPage + 1 > allPage) {
		layer.alert("当前已经是最后一页");
		return;
	}

	currPage = currPage + 1;
	selectCheckFile();
};

function pageback() {
	if (currPage - 1 == 0) {
		layer.alert("当前已经是第一页");
		return;
	}
	
	currPage = currPage - 1;
	selectCheckFile();
};

//发件操作
var uId = 0;
var eId = 0;
var Money = 0;
function sendfile(index,uid,eid,money){
console.log('发件');
 uId = uid;
 eId = eid;
 Money = money;
 var user = List[index];
$('#sendfile').modal({
	show:true,//展示出来
	moveable:true,//可移动
	rememberPos:true,//记住移动位置
	backdrop:'static'//背景遮罩
	
});
//拿到单选框对象
var radios = document.getElementsByName('status1');
for(var i = 0; i<radios.length; i++){
	if(radios[i].value == user.sendState){//判断value和我们的值是否相等
		
		radios[i].checked = true;		
		
	}
}
}
function confirm(){
console.log('用户id:'+uId);
console.log('快件id:'+eId);
console.log('运费:'+Money);
var content = $('#content').val();
console.log('运费:'+content);
$.ajax({
	url:ajax_host+'toSend.act',
	type : 'post',
	dataType:'JSON',
	data:{
		uid : uId,
		eid : eId,
		money : Money,
		content : content,
		status : val('status1','radio')
	},
	
	success:function(resp){
		if(resp.success){
			layer.alert(resp.success);
			selectCheckFile();
			$('#sendfile').modal('hide', 'fit');
		}else{
			layer.alert(resp.errmsg);
			$('#sendfile').modal('hide', 'fit');
		}
	}
})
}


//退单操作
function tuidan(uid,eid,money){
console.log('进行退单');

layer.confirm("确定对该用户进行退单吗？", {
	btn: ['确定','取消'] //按钮
   ,skin: 'layui-layer-lan'
   ,closeBtn: 0
   ,anim: 3 //动画类型
},function(){


$.ajax({
	url:ajax_host+'chargeback.act',
	type:'post',
	dataType:'JSON',
	data:{
		uid : uid,
		eid : eid,
		money : money
	},
success:function(resp){
	if(resp.success){
		//删除数组的元素
		layer.alert('退单成功');
		selectCheckFile();
		
	}else{
		layer.alert(resp.errmsg);
	}
	
}
})


});

}